#include "ebm_env.h"
#include "bconn.h"
#include "ebm_ts_parse.h"
#include "encode.h"
#include "user_cfg.h"

#define MP3_LEN 1024   
#define BLICK_T (100) //阻塞100ms
#define BLICK_S (10) //*10次，对应BLICK_T 100ms
#define DBG printf



#define NEVIF_CMD_PGNUM  7198   /*节目号 syscfg_get_serid*/
#define NEVIF_PG_TOUT  30   /*节目超时*/
#define NEVIF_PG_IP   p_readcfg->ebm.pg_ip
#define NEVIF_PG_PORT p_readcfg->ebm.pg_port

static void _speed_ctl_reset(void){};
static void _speed_ctl_inc(void){}; 
static void _speed_ctl_dec(void){};


/*发送阻塞10ms*/

static struct bsockt ebm_pgNet_handle = \
BCONN_INIT_STRUCTURE("ebm pg", 0, 0, BLICK_S, BLICK_T); 

static void _pgRec(struct bsockt *b)
{
    unsigned char data[1460]; unsigned short len = 1460;
    int ret = 1460, cnt = 100;
    while(cnt-- && ret >= 1460) {
        ret = bconn_rec(b, data, len);
        if(ret) {
            printf("%s rec %d bytes\n", b->name, ret);
            mp3_buf_write(data, ret);
        }else {
            usleep(200*1000);
        }
    }
}

static int _pgSend_passwd(int fd)
{
    const unsigned char load[2] = {0, 0};
    int wlen = write(fd, load, 2);
    if(wlen <=0 ){
        printf("pg send passwd err\n");
        ebm_pgNet_restart();
    }
    return wlen;
}


static void _pgSend(struct bsockt *b)
{
    if(PEND_T_S()){
        player_manage(&current_pg);
        SLEPP_T_S(1);  
    }
}






void ebm_pgNet_start(void) {
    bconn_connect(&ebm_pgNet_handle, NEVIF_PG_IP, NEVIF_PG_PORT);
}

void ebm_pgNet_stop(void)
{
    bconn_stop(&ebm_pgNet_handle);
}

void ebm_pgNet_restart(void)
{
    bconn_restart(&ebm_pgNet_handle);
}

void *ebm_pg_task(void *parm)
{
    struct bsockt *b = &ebm_pgNet_handle;
    ebm_pgNet_start();
    bconn_reg_LoadFuc(b, _pgSend_passwd);
	while(1)
	{
		bconn_machine(b);
        if(bconn_isConnect(b)) {
            _pgRec(b);
            _pgSend(b);
            
        }
        usleep(10*1000);
	}
	return 0;
}


void PgCmd_Stop(PgInfo* pg) {
    pg->cmdLv = 0;
    memset(pg->logicAddr, 0, 6);
    pg->swSta = pgCmdStop;
    pg->emFlag = 0;
    pg->pgId = 0;
    ebm_play_cmd_stop();
}

void PgCmd_Reinit(PgInfo* pg) {
    PgCmd_Stop(pg);
    pg->lastPgid = 0;
	pg->playwcnt = 0;
	pg->cmdwcnt = 0;
}


void PgMsg_Start(PgInfo* pg) {
    struct bsockt *b = &ebm_pgNet_handle;
    unsigned char buf [3];
	buf[0] = 0x0A;
	buf[1] = pg->pgId >> 8;
	buf[2] =pg->pgId&0xff;
	bconn_send(b, buf, 3);
    DBG("节目开始\n");
}



void PgMsg_Stop(PgInfo* pg) {
    struct bsockt *b = &ebm_pgNet_handle;
    unsigned char buf [3] = {0x0b, 0x00, 0x00};
	pg->pgId = 0;
	pg->lastPgid = 0;
	bconn_send(b, buf, 3);
    DBG("节目停止\n");
}

void PgMsg_Hearttick(PgInfo* pg) {
    static unsigned int ctl = 0;
    if((ctl++)%3 == 0) {
        struct bsockt *b = &ebm_pgNet_handle;
        unsigned char buf [3] = {0x01, 0x00, 0x00};
    	bconn_send(b, buf, 3);
        //DBG("PgMsg_Hearttick\n");
    }
}

void player_manage(PgInfo* pg) {
    unsigned short current_id;
	if (pg->playwcnt > 0){
		pg->playwcnt--;
		if (pg->playwcnt == 0) {
			DBG("节目心跳超时\n");
			PgMsg_Stop(pg);
            PgCmd_Stop(pg);
		}
	}
	if (pg->cmdwcnt > 0){
		pg->cmdwcnt-- ;
		if (pg->cmdwcnt == 0) {
            DBG("指令心跳超时\n")    ;
			PgMsg_Stop(pg);
			PgCmd_Stop(pg);
		}
	}
	
	current_id = pg->pgId;
	if (current_id != 0 && pg->lastPgid == 0) {
		PgMsg_Start(pg);
	}else if (current_id == 0 && pg->lastPgid != 0) {
		PgMsg_Stop(pg);
	}else if (current_id != 0 && pg->lastPgid != current_id) {
		PgMsg_Stop(pg);
		PgMsg_Start(pg);
	}else {
        PgMsg_Hearttick(pg);
    }
	pg->lastPgid = current_id;
}


unsigned char ebm_pgNet_sta(void)
{
    struct bsockt *b = &ebm_pgNet_handle;
    return bconn_isConnect(b);
}

unsigned char ebm_pgNet_stastr(void)
{
    const char* str[] = {
        "未连接",
        "正常" 
    };
    unsigned char sel = adp_cmdNet_sta();
    sel = MIN(1, sel);
    return str[sel];
}

unsigned int ebm_pgNet_speed(void)
{
    /*发送速率*/
    struct bsockt *b = &ebm_pgNet_handle;
    return bconn_speed_s(b);
}

const char* visit_ebmPg_net_speed(void)
{
extern unsigned char nstar_encode_fifodep(void);

	static char buf[55];
	int idx=0;
	if(0 == ebm_pgNet_sta()){
		sprintf(buf, "待机中");
	}
	else{
		unsigned int speed= ebm_pgNet_speed();
		idx= sprintf(buf, "已连接:");
		if(speed <= 1024)
			idx+= snprintf(&buf[idx], 55-idx, "%dB/s, 缓冲:%d%%", speed ,nstar_encode_fifodep() );
		else 
			idx+= snprintf(&buf[idx], 55-idx, "%dKB/s, 缓冲:%d%%", speed/1024 ,nstar_encode_fifodep() );
	}
	return buf;
}

unsigned char visit_ebmPg_emflag(void) {
    return current_pg.emFlag;
}







